package org.lansir.af.ui;

import android.app.Activity;
import android.app.Application;
import android.content.Intent;
import android.os.Bundle;
import android.view.Window;
import android.view.WindowManager;
import org.lansir.af.database.DataTable;
import org.lansir.af.database.Database;
import org.lansir.af.util.Prefs;
import org.lansir.af.util.Utils;

/**
 * 
 * @author 蓝智宏
 *
 */
public abstract class CommonActivity extends Activity {
	
	// enum
	public enum StartMode {
		Default, EDIT, NEW, VIEW
	};
	
	public enum Action {
		Default, ShowMsg
	};
	
	public enum SnoozeAction {
		Snooze, Clear;
		public static int convertToInt(SnoozeAction aAction){
			switch(aAction){
			case Snooze:
				return 0;
			case Clear:
				return 1;
			}
			return 1;	
		}
		
		public static SnoozeAction convertFromInt(int iAction){
			switch(iAction){
			case 0:
				return Snooze;
			case 1:
				return Clear;
			}
			return Clear;	
		}
	};
	
	public class ViewType{
		public static final int NONE = 0;
		public static final int DAY = 1;
		public static final int WEEK = 2;
		public static final int MONTH = 3;
		public static final int TODAY_ALARM = 4;
		public static final int GROUP = 5;
	}
	
	public static final String requestBundleRowId = "RowId";
	public static final String requestBundleTableUpdated = "TableUpdated";
	public static final String requestBundleActivityOrganizer = "ActvitiyOrganizer";
	public static final String requestBundleActivityTitle = "ActvitiyTitle";
	public static final String requestBundleActivityLocation = "ActvitiyLocation";
	public static final String requestBundleActivityDate = "ActvitiyDate";
	public static final String requestBundleActivityTime = "ActvitiyTime";
	public static final String requestBundleActivityRingerMode = "ActvitiyRingerMode";
	public static final String requestBundleActivityAlarm = "ActvitiyAlarm";
	public static final String requestBundleActivityRepeat = "ActvitiyRepeat";
	public static final String requestBundleActivityParticipator = "ActvitiyParticipator";
	
	public static final int REQUEST_CODE_DEFAULT = 0;
	public static final int RESULT_ERROR = -5;
	
	// fields
	private StartMode startMode = StartMode.Default;
	private Bundle bundleDataStartup = new Bundle();
	protected Bundle bundleOtherDataStartup = new Bundle();
	protected Bundle bundleOtherDataClose = new Bundle();
	protected Bundle bundleDateValues = new Bundle();

	protected Utils utils;
	protected Prefs prefs;
	protected Database userdb = null;
	
//	protected ActivityFairyApplication mActivityFairyApplication;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);  
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		super.onCreate(savedInstanceState);
		this.utils = new Utils(this);
		this.prefs = new Prefs(this);
		this.userdb = new Database(this);
		Application app = getApplication();
//		if(app instanceof ActivityFairyApplication)
//			mActivityFairyApplication = (ActivityFairyApplication) app;
	}
	
	@Override
	protected void onDestroy() {
		super.onDestroy();
		this.userdb.close();
	}



	protected void init(int layoutId) {
		setContentView(layoutId);
		initViews();
		initStates();
	}
	
	
	
	protected abstract void initViews(); 
	
	protected abstract void initStates();
	

	public StartMode getStartMode() {
		startMode = StartMode.Default;
		String sAction = getIntent().getAction();
		if (sAction.contains("ACTION_MODE_NEW"))
			startMode = StartMode.NEW;
		if (sAction.contains("ACTION_MODE_EDIT"))
			startMode = StartMode.EDIT;
		if (sAction.contains("ACTION_MODE_VIEW"))
			startMode = StartMode.VIEW;
		return startMode;
	}
	
	/**
	 * 如果为新的APPT，RowId=-1
	 * @return
	 */
	public long requestedRowId() {
		return getIntent().getExtras().getLong(CommonActivity.requestBundleRowId);
	}
	
	public boolean openDataForEdit(DataTable data) {
		long lRowId = requestedRowId();
		Database.Result result = data.getRowDataForEdit(lRowId);
		if (result == Database.Result.Success) {
			// save date values for change test
			return true;
		} else {
			// return fail result for caller
			int iMsgId = Database.getErrDesc(result);
			Bundle extras = putBundledMessage(iMsgId);
			setIntentResult("", RESULT_CANCELED, extras);

			return false;
		}
	}

	private Bundle putBundledMessage(int iMsgId) {
		Bundle extras = new Bundle();
		extras.putInt("msgResStrId", iMsgId);
		extras.putInt("msgType", Utils.MSGTYPE_ERROR);
		extras.putString("action", Action.ShowMsg.toString());
		return extras;
	}
	
	public void openActivity(String sAction){
		Intent intent = new Intent(sAction);
		bundleDataStartup.clear();
		bundleDataStartup.putAll(bundleOtherDataStartup);
		intent.putExtras(bundleDataStartup);
		
		startActivity(intent);
	}
	
	public void openActivityForResult(int iActivityRequestCode, String sAction,
			long lRowId) {
		bundleDataStartup.clear();
		bundleDataStartup.putLong(CommonActivity.requestBundleRowId, lRowId);
		bundleDataStartup.putAll(bundleOtherDataStartup);
		Intent it = new Intent(sAction);
		it.putExtras(bundleDataStartup);
		startActivityForResult(it, iActivityRequestCode);
	}
	
	public void openActivityForResult(int iRequestCode,String sAction){
		Intent intent = new Intent(sAction);
		bundleDataStartup.clear();
		bundleDataStartup.putAll(bundleOtherDataStartup);
		intent.putExtras(bundleDataStartup);
		
		startActivityForResult(intent, iRequestCode);
	}
	
	public void closeActivity(String sAction, int sResultCode) {
		setIntentResult(sAction, sResultCode, bundleOtherDataClose);
		finish();
	}
	
	public void closeActivity(DataTable data) {
		bundleOtherDataClose.putString(requestBundleTableUpdated, data.getTableName());

		setIntentResult("", RESULT_OK, bundleOtherDataClose);
		finish();
	}
	
	
	protected void setIntentResult(String action, int resultCode, Bundle bundle) {
		Intent intentData = new Intent(action);
		intentData.putExtras(bundle);
		setResult(resultCode, intentData);
	}
	
	public static Bundle getIntentExtras(Intent data) {
		// data is null, when activity cancelled by BACK BUTTON
		if (data != null) {
			Bundle extras = data.getExtras();
			if (extras != null) {
				return extras;
			}
		}
		return null;
	}
	
	public boolean dateValuesChanged(Bundle data) {
		return false;
	}

	public Bundle getBundleOtherDataStartup() {
		return bundleOtherDataStartup;
	}
	
	
}

